package cn.cdtu.tibetplan.controller;

import java.util.List;

import cn.cdtu.tibetplan.pojo.PageEnd;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.Result;
import cn.cdtu.tibetplan.pojo.Route;
import cn.cdtu.tibetplan.util.Tools;
import com.alibaba.fastjson.JSON;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.cdtu.tibetplan.service.RouteService;

import javax.jws.WebParam;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/process")
public class RouteController {

    @Autowired
    private RouteService routeService;

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<Route> findAll() {
        return routeService.findAll();
    }

    @RequestMapping("/list")
    public String list(Model model){
//        List<Route> list = routeService.findAll();
//        model.addAttribute("routes",list);
        return "redirect:search?currentPage=1&pageSize=10";
    }

    @RequestMapping("/search")
    public String search(Route route, int currentPage, int pageSize, Model model) {
        PageResult result = routeService.findPage(route, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "/Process/index";
    }

    @RequestMapping("/addProcess")
    public String addProcess(){
        return "/Process/add";
    }

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(int page, int rows) {
        return routeService.findPage(page, rows);
    }

    @RequestMapping("/edit")
    public String edit(Long id,Model model){
        Route route = routeService.findOne(id);
        model.addAttribute("process",route);
        return "/Process/edit";
    }
    /**
     * 增加
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Route route) {
        try {
            route.setCreatetime(Tools.getTime());
            routeService.add(route);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param route
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public Result update(Route route) {
        try {
            route.setUpdatetime(Tools.getTime());
            routeService.update(route);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public Route findOne(Long id) {
        return routeService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            routeService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

//    /**
//     * 查询+分页
//     *
//     * @param
//     * @param page
//     * @param rows
//     * @return
//     */
//    @RequestMapping("/search")
//    public PageResult search(@RequestBody Route route, int page, int rows) {
//        return routeService.findPage(route, page, rows);
//    }

    @RequestMapping("/getRoute")
    public String getRoute(long routeId, Model model){
        Route route = routeService.findOne(routeId);
        final String[] noods = route.getInfo().split("-");
        model.addAttribute("routeName",route.getTitle());
        model.addAttribute("noods",noods);
        return "/index/processshow";
    }

}
